package com.zhsj.OkeHomeConsumption.ui.activity.bussiness;

import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.other.QRCodeUtil;
import com.zhsj.OkeHomeConsumption.ui.activity.bussiness.mvp.ReceiptQRcodePresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.bussiness.mvp.ReceiptQRcodeUIInterFace;
import com.zhsj.OkeHomeConsumption.utils.SharedPreferanceUtils;


import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;


import java.util.Hashtable;

/**
 * 商家页面---收款二维码
 * */

public class ReceiptQRcodeActivity extends MyActivity implements ReceiptQRcodeUIInterFace {

    private ImageView qr_code = null;

    private ReceiptQRcodePresenter minePersonPresenter;

    private FormalUserInfo userinfo = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_receiptqrcode;
    }

    @Override
    protected int getTitleId() {
        return R.id.tb_copy_title;
    }

    @Override
    protected void initView() {
        minePersonPresenter = new ReceiptQRcodePresenter(getActivity(), this);
        qr_code = findViewById(R.id.qr_code);

    }

    @Override
    protected void initData() {
        userinfo = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        minePersonPresenter.IncomeGet(userinfo.getId());


    }



    @Override
    public void IncomeGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                String jsonarr = jsonOBJ.optString("data");
                // 数据对接正常
                //生成二维码
                if(jsonarr!=null){
                    Bitmap bitmap = QRCodeUtil.createQRImage(jsonarr);
                    Glide.with(getActivity()).load(bitmap).placeholder(R.drawable.zhanweitu_bg).into(qr_code);
                }
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("MerchantTypeGet", "获取商家二维码");
        }
    }
}
